<html>
	<head>
		<meta http-equiv="Content-type" content="text/html; charset=utf-8">
		<title>calendar_test</title>
		<link rel="stylesheet" href="<?php echo BASE_URL(); ?>assets/css/reset.css" type="text/css" media="screen">
		<link rel="stylesheet" href="<?php echo BASE_URL(); ?>assets/css/master.css" type="text/css" media="screen">
		<script src="<?php echo BASE_URL(); ?>assets/js/mootools-release-1.11.js" type="text/javascript" charset="utf-8"></script>
		<script src="<?php echo BASE_URL(); ?>assets/js/nerdycalendar-0.5.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript" charset="utf-8">
			
			var hourHeight = 25;
			var dayWidth = 400;
			
			var myDayArray = new Array();
			var myNerdyCalendar = new nCal();
			
			window.addEvent('domready', function(){
			
				var container = $('day');
				var entries = $$('div.entry');
				
				myNerdyCalendar.setup(container, myDayArray)
				
				/*
				 * entries.each Gedoens nur als Uebergangsloesung
				 * macht die DIVs aus PHP interaktiv
				 */
				entries.each(function(div) {
					
					new Drag.Move(div.id, {
						'container': $('day'),
						'grid': hourHeight / 2,
						'droppables': entries,
						'modifiers': {
							x: false,
							y: 'top'
						},
						onComplete: function(){
							
							arrayAlert();
						
						}
						
					});
					
					$(div).makeResizable({
					
						modifiers: {
							x: false,
							y: 'height'
						},
						handle: 'handler' + div.id,
						grid: hourHeight / 2,
					
					});
					
				});
				
			});
			
			function addEntry(event){
				
				var eventStart = ((event.client.y / (hourHeight / 2)).round()) * (hourHeight / 2);
				
				var url = "<?=BASE_URL(); ?>index.php/application/addEntry";
				
				new Ajax(url, {
					postBody: 'start=' + eventStart,
					onComplete: function(request){
						drawEntry(request)
					}
				}).request();
			}
			
			function drawEntry(request){
				
				var requestdata = request.split(',');
				myNerdyCalendar.entryCreate(requestdata[0], dayWidth, requestdata[1], requestdata[2]);
			}
			
			function deleteEntry(targetId){
				
				new Ajax('<?=BASE_URL(); ?>index.php/application/deleteEntry', {
					postBody: 'id=' + targetId
				}).request();
				myNerdyCalendar.entryDelete(targetId);
			}
			
			// ############################################################
			// ############### ARRAY SORT STUFF TESTING ###################
			// ############################################################
			
			function assozArrayObj(key, value){
				
				this.key = key;
				this.value = value;
			}
			
			function assozArraySort(a, b){
				
				return a.key - b.key;
			}
			
			function arrayAlert() {
			
				var entriesArray = new Array();
				var entries = $$('div.entry');
				
				// aus allen events wird ein array gemacht, mit top als key und id als value
				entries.each( function(e) {
					
					entriesArray.push( new assozArrayObj( parseFloat( e.style.top ), e.id ) );
					
				})
				
				// das array wird nach key sortiert, oberste events kommen zuerst 
				entriesArray.sort( assozArraySort );
				
				var entriesStartStopArray = new Array();
				
				entriesArray.each( function(e) {
				
					// alert( e.key + " / " + e.value );
					
					entriesStartStopArray.push ( new assozArrayObj( parseFloat( $(e.value).style.top ), 'start' ) );
					entriesStartStopArray.push ( new assozArrayObj( parseFloat( $(e.value).style.top ) + parseFloat( $(e.value).style.height ), 'end' ) );
					
				});
				
				entriesStartStopArray.sort( assozArraySort );
				
				var entriesLevels = new Array();
				
				i = 0;
				act = 0;
				
				entriesStartStopArray.each( function(e) {
				
					// alert( e.key + " / " + e.value );
					
					if ( e.value == 'start' ) {
						
						$(entriesArray[act].value).setHTML('  i:' + i + ', act:' + act); 
						
						
						i++;
						entriesLevels.push ( new assozArrayObj( e.key, i ) );
						
						
						
						if ( i > 1) {
							
							for ( ll = act - (i-1); ll < i ; ll++ ) {
								
								alert(entriesArray[ll].value);
								
								//$(entriesArray[ll].value).setStyle('background-color', '#face8f');
								
							}
							
							// alert( entriesArray[act].value + ' ' + entriesArray[act-1].value );
							
							//$(e.value).setStyle('background-color', '#face8f');
							
						}
						
					} else {
						
						i--;
						entriesLevels.push ( new assozArrayObj( e.key, i ) );
						
					}
					
					act++;
					
				});
				
				
				
				/*
				entriesLevels.each( function(e) {
				
					alert( e.key + " => " + e.value );
					
				});
				*/
				/*
				entries.each( function(e) {
				
					// alert( e.key + " / " + e.value );
					
					//alert( 'hallo');
					
					entriesLevels.each( function(a) {
						
						//alert( e.style.top + " / " + a.key );
					
						if ( (parseFloat(e.style.top) >= a.key) ) {
							
							e.setStyle('background-color', '#face8f');
							
						}
					
					});
					
					
					
				});
				
				*/
				
				
				
			}
			
			// ############################################################
			// ############### ARRAY SORT STUFF TESTING ###################
			// ############################################################
		</script>
	</head>
	<body>
		<?php
			echo $content;
		?>
		<script type="text/javascript" charset="utf-8">
			$('day').addEvent('dblclick', addEntry.bindWithEvent('day'));
		</script>
	</body>
</html>
